function getDB() {
    var database = openDatabase(
            "NotesDB",
            "1.0",
            "MiniNote数据库",
            5000000
        );
    return database;
}

function createNoteTable(db, callback) {
    var sql = "CREATE TABLE IF NOT EXISTS notes " +
        "(id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT, date DATE, imageContent BLOB, category NVARCHAR(15));" 
    db.transaction(function (tx) {
        tx.executeSql(
            sql,
            [], 
            callback,
            statementErrorCallback
        );
    }, transactionErrorCallback);
}

function statementErrorCallback(transaction, error) {
    alert("错误: " + error.message);
}

function transactionErrorCallback(error) {
    alert("错误: " + error.message);
}

function insertNote(db, note, callback) {
    var sql = "INSERT INTO notes (content, date, imageContent, category) VALUES (?,?,?,?);"
    db.transaction(function (tx) {
        tx.executeSql(
            sql,
            [note.get("content"), note.dateInfo(), note.get("imageContent"), note.get("category")], 
            callback,
            statementErrorCallback
        );
    });
}

function updateNote(db, note, callback) {
	var sql ="update notes set content=?,imageContent=?,category=? where id=?" ;   
    db.transaction(function (tx) {
        tx.executeSql(
            sql,
            [note.get("content"), note.get("imageContent"), note.get("category"), note.id], 
            callback,
            statementErrorCallback
        );
    });
}

function getNotes(db, callback) {
    var sql = "SELECT * FROM notes ORDER BY id DESC;";
    db.transaction(function (tx) {
        tx.executeSql(
            sql,
            [],
            function(transaction, resultSet) {
                var rows = resultSet.rows;
                var noteList = new NoteList();
                for (var i = 0; i < rows.length; i++) {
                    var note = Note.fromJSONLikeData(rows.item(i));
                    noteList.add(note);
                }
                callback(noteList);
            },
            statementErrorCallback
        );
    });
}

function getNoteById(db, id, callback) {
    var sql = "SELECT * FROM notes WHERE id=?;";
    db.transaction(function (tx) {
        tx.executeSql(
            sql,
            [id],
            function(transaction, resultSet) {
                var rows = resultSet.rows;
                var note = null;
                for (var i = 0; i < rows.length; i++) {
                    note = Note.fromJSONLikeData(rows.item(i));
                }
                callback(note);
            },
            statementErrorCallback
        );
    });
}

function deleteNoteById(db, id, callback) {
    var sql = "DELETE FROM notes WHERE id=?;";
    db.transaction(function (tx) {
        tx.executeSql(
            sql,
            [id],
            callback,
            statementErrorCallback
        );
    });
}

function cleanDB(db, callback) {
    var sql = "DELETE FROM notes;";
    db.transaction(function (tx) {
        tx.executeSql(
            sql,
            [],
            callback,
            statementErrorCallback
        );
    });
}

function restoreDB(db, noteList) {
    var sql = "INSERT INTO notes (id, content, date, imageContent, category) VALUES (?,?,?,?,?);"
    db.transaction(function (tx) {
        noteList.forEach(function (note) {
            tx.executeSql(
                sql,
                [note.id, note.get("content"), note.dateInfo(), note.get("imageContent"), note.get("category")], 
                null,
                statementErrorCallback
            );
        });
    });
}